package com.liusu.spider.js;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class String_ {


    /**
     * 反向切片 返回对应索引的片段
     * @param content
     * @param sign
     * @param index
     * @return
     */


    public static String rsplit(String content,String delimiter){
        return rsplit(content,delimiter,-1,-1);
    }
    public static String rsplit(String content,String delimiter,int index){
        return rsplit(content,delimiter,-1,index);
    }
    /**
     *
     * length 8
     *
     * @param content
     * @param sign
     * @param count
     * @param index
     * @return
     * https://videozm.whqhyg.com:8091/20210601/LYAF2ODp/1000kb/hls/index.m3u8
     */
    public static String rsplit(String content,String delimiter,Integer count,int index){
        String[] array = content.split(delimiter);
        int length = array.length;

        String[] list = null;

        if(count > 0){
            list = new String[count+1];

            list[0] = StringUtils.join(array,delimiter,0,length-count);
            int k = length-count;
            for (int i = 1; i < list.length; i++) {
                list[i] = array[k];
                k++;
            }
        }else{
            list = array;
        }
        if(index < 0){
            index = list.length+index;
        }
        return list[index];
    }

    public static Map<String,String> string_to_map(String line){

        Map<String,String> m = new HashMap<>();
        String[] split = line.split(";");
        for (String cookie : split) {
            String[] kv = cookie.split("=",2);
            m.put(kv[0].trim(),kv[1].trim());
        }
        return m;
    }
}
